/**	用户编辑界面
 * 	@author		donghj
 * 	@since		2013.8.11
 * ====================================================================
 * 入参:
 * 		record			(用户record)					默认值: null
 * 		gridStore		(用户列表store)				默认值: null
 * 		isAddNew		(判断是新增还是修改)			默认值: true
 * ====================================================================
 */
 
Ext.define('hkyg.view.EdtUserInfo',{
	extend: 'Ext.window.Window',
	alias : 'widget.edt_UserInfo',
	layout: 'fit',
	width: 300,
	record: null,
	gridStore: null,
	isAddNew: true,
	listeners:{
		afterrender: function(window){
			window.down('form').loadRecord(window.record);
			var who = null;
			if(window.record.get('authority') == 0){
				who = '普通用户';
			}else if(window.record.get('authority') == 1){
				who = '管理员';
			}
			if(window.isAddNew == true){
				window.setTitle('新增'+who);
			}else if(window.isAddNew == false){
				window.setTitle('修改'+who);
			}
		}
	},
	initComponent: function(){
		this.buttons = [{
			text: '保存',
			iconCls: '',
			handler: function(btn){
				btn.up('edt_UserInfo').doSave();
			}
		},{
			text: '退出',
			iconCls: '',
			handler: function(btn){
				btn.up('edt_UserInfo').close();
			}
		}];
		Ext.apply(this,{
			items:[{
				xtype: 'form',
				bodyPadding: 10,
				frame: true,
				defaults:{
			    	labelAlign: 'right',
			    	anchor: '95%',
	    			labelWidth: 90,
	    			labelStyle: 'font-weight:bold',
	    			msgTarget: 'under',
		            autoFitErrors: true
				},
				items:[{
					xtype: 'fieldcontainer',
					layout: 'vbox',
					defaults:{
				    	labelAlign: 'right',
				    	anchor: '95%',
		    			labelWidth: 90,
		    			labelStyle: 'font-weight:bold',
		    			msgTarget: 'under',
			            autoFitErrors: true,
			            width: '100%',
						flex: 1
					},
					items:[{
						xtype: 'textfield',
						fieldLabel: '用户名',
						readOnly: this.isAddNew == true ? false : true,
		    	 		blankText: '用户名不允许为空！',
		    	 		enforceMaxLength: true,
		    	 		maxLength: 30,
						name: 'user_name'
					},{
						xtype: 'textfield',
						fieldLabel: '密码',
						inputType: 'password',
		    	 		blankText: '密码不允许为空！',
		    	 		enforceMaxLength: true,
		    	 		maxLength: 50,
						name: 'password'
					},{
						xtype: 'textfield',
						fieldLabel: '真实姓名',
		    	 		enforceMaxLength: true,
		    	 		maxLength: 30,
						name: 'real_name'
					},{
						xtype: 'radiogroup',
						fieldLabel: '性别',
			            items:[
			            	{boxLabel: ' 男 ', name: 'gender', inputValue: 0},
			                {boxLabel: ' 女 ', name: 'gender', inputValue: 1}
			                
			            ]
					}]
				}]
			}]
		});
		this.callParent(arguments);
	},
	doSave: function(){
		var edt_UserInfo = this;
		var edtForm = edt_UserInfo.down('form');
		if(edtForm.getForm().isValid() && edtForm.getForm().isDirty()){
			var user_rec = edtForm.getForm().getRecord();
			edtForm.getForm().updateRecord(user_rec);
//			user_rec.set('password', hex_md5(user_rec.get('password')));
			var url;
			if(edt_UserInfo.isAddNew == true){
				url = 'hkyg/Background.do?method=addUser';
			}else{
				url = 'hkyg/Background.do?method=updateUser';
			}
			Ext.Ajax.request({
				url: url,
				params: user_rec.getData(),
				success: function(response){
					edt_UserInfo.gridStore.load();
					edt_UserInfo.close();
				}
			});
		}
	}
});